<template>
    <div class="dict">
        <el-card class="!border-none" shadow="never">
          <el-form class="mb-[-16px]" :model="formData" inline>
            <el-form-item label="字典项">
              <el-input v-model="formData.type" class="w-[280px]" clearable @keyup.enter="resetPage" />
            </el-form-item>
            <el-form-item label="字典值">
              <el-input v-model="formData.value" class="w-[280px]" clearable @keyup.enter="resetPage" />
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="resetPage">查询</el-button>
              <el-button @click="resetParams">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      <el-card v-loading="pager.loading" class="mt-4 !border-none" shadow="never">
        <el-button v-perms="['common:dict:add']" type="primary" @click="handleAdd">
          <template #icon>
            <icon name="el-icon-Plus" />
          </template>
          新增
        </el-button>
        <div class="mt-4">
          <el-table :data="pager.lists" size="large" stripe>
            <el-table-column label="ID" prop="id" min-width="60" />
            <el-table-column label="字典项" prop="type" min-width="100" />
            <el-table-column label="字典值" prop="value" min-width="100" />
            <el-table-column label="创建时间" prop="createTime" min-width="180" />
            <el-table-column label="操作" width="120" fixed="right">
              <template #default="{ row }">
                <el-button v-perms="['common:dict:edit']" type="primary" link @click="handleEdit(row)">
                  编辑
                </el-button>
                <el-button v-perms="['common:dict:del']" type="danger" link @click="handleDelete(row.id)">
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="flex mt-4 justify-end">
          <pagination v-model="pager" @change="getLists" />
        </div>
      </el-card>
      <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>

<script setup lang="ts">
import {usePaging} from "@/hooks/usePaging";
import {dictDelete, dictLists} from "@/api/common/dict";
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'

const editRef = shallowRef<InstanceType<typeof EditPopup>>()

const formData = reactive<any>({
  name: '',
  type: ''
})
const showEdit = ref(false)
const { pager, getLists, resetParams, resetPage } = usePaging({
  fetchFun: dictLists,
  params: formData
})

const handleAdd = async () => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('edit')
  editRef.value?.setFormData(data)
}

const handleDelete = async (id: number) => {
  await feedback.confirm('确定要删除？')
  await dictDelete({ id })
  feedback.msgSuccess('删除成功')
  getLists()
}

onMounted(() => {
  getLists()
})
</script>
